package com.scuola.scuoladocente;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class avvisiTab extends Activity {

	private JSONArray parsedList = null;
	private JSONParser parser = new JSONParser();
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_avvisi_tab);
        
	    ListView lv = (ListView)findViewById(R.id.lv_avvisi);
	    ArrayList<String> lvItems = new ArrayList<String>();
	    
        try {
        	AsyncTask<Void, Void, String> getAvvisoResponse = new getAvviso().execute();
        	parsedList = (JSONArray) parser.parse(getAvvisoResponse.get());
			
			for(int i = 0; i < parsedList.size(); i++ ){
				JSONObject obj = (JSONObject) parser.parse(parsedList.get(i).toString());
				lvItems.add((obj.get("cognome").toString()+" "+obj.get("nome").toString()));
			}
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvItems);
			lv.setAdapter(adapter);
		
		}catch (Exception e) {
			e.printStackTrace();
		}
        
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				try {
					Intent LetturaAvvisoActivity = new Intent(getApplicationContext(), LetturaAvvisoActivity.class);
					JSONObject selectedObj = (JSONObject) parser.parse(parsedList.get(arg2).toString());
					LetturaAvvisoActivity.putExtra("cognome", selectedObj.get("cognome").toString());
					LetturaAvvisoActivity.putExtra("nome", selectedObj.get("nome").toString());
					LetturaAvvisoActivity.putExtra("id_mittente", selectedObj.get("id_mittente").toString());
					LetturaAvvisoActivity.putExtra("data", selectedObj.get("data").toString());
					LetturaAvvisoActivity.putExtra("descrizione", selectedObj.get("descrizione").toString());	
					LetturaAvvisoActivity.putExtra("idclasse", getIntent().getStringExtra("idclasse"));
					LetturaAvvisoActivity.putExtra("idutente", getIntent().getStringExtra("idutente"));
	            	startActivityForResult(LetturaAvvisoActivity, 0);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
        
	 }
	
	private class getAvviso extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... arg0) {
			try{	
				LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();
				hm.put("classId", getIntent().getStringExtra("idclasse"));
				hm.put("docenteId", getIntent().getStringExtra("idutente"));
				String strResponse = operations.wsCall("getAvviso", hm).toString();
				return strResponse;
				
			} catch (Exception e) {
				System.err.println("getAvviso errore (Exception)");
				e.printStackTrace();
				return null;
			}
		
		}
    }
	 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_avvisi_tab, menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.crea_avviso:
            	Intent CreaAvvisiActivity = new Intent(getApplicationContext(), CreaAvvisiActivity.class);
            	CreaAvvisiActivity.putExtra("idclasse", getIntent().getStringExtra("idclasse"));
            	CreaAvvisiActivity.putExtra("idutente", getIntent().getStringExtra("idutente"));
            	startActivityForResult(CreaAvvisiActivity, 0);
                break;
        }
		return true;
    }

}
